const router = require('express').Router()
const {db,pool}=require('../db/db')
const commentSQL=require('../db/commentSQL')
const userSQL=require('../db/userSQL')
const addcomment = async (userId,goodsId,comment_content) => {
  await pool.query(commentSQL.insert,[userId,goodsId,comment_content,new Date()],(err,result)=>{undefined
        if(err) throw err;
        console.log(result);
  });
  return 'ok'
}
const getCommentList = async (goods_id) => {
  let result = await db(commentSQL.queryAll, [])
  let user = await db(userSQL.queryAll, [])
  let data=result.filter(item => item.goods_id === Number(goods_id))
  let data1=JSON.stringify(data)
  let get_comment=JSON.parse(data1)
  let str=''
  get_comment.forEach(item=>{
    let userId=item.user_id
    let username=user.find(item=>item.id===userId).username
    let val=JSON.stringify(item)
    str+=val.slice(0,-1)+',"username":"'+username+'"},'
  })
  let info1='['+str.slice(0,-1)+']'
  let info=JSON.parse(info1)
  return info
}

module.exports = {
  addcomment,
  getCommentList
}
